package com.jeeplus.modules.monitor.task;

import com.jeeplus.common.utils.JdbcUtils;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.modules.monitor.entity.ScheduleJob;
import com.jeeplus.modules.monitor.entity.Task;
import com.jeeplus.modules.monitor.service.jobService;
import com.jeeplus.modules.tools.utils.MultiDBUtils;
import com.jeeplus.modules.tools.utils.SpringUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jca.cci.connection.ConnectionHolder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.SmartDataSource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.resource.ResourceException;
import javax.sql.DataSource;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Administrator
 * @title: TaskProcedure
 * @projectName jeeplus
 * @description: TODO
 * @date 2019/8/814:14
 */
@Transactional(readOnly = true)
public class TaskProcedure extends Task {
    /*日志*/
    protected Logger logger = LoggerFactory.getLogger(getClass());
    JdbcTemplate jdbcTemplate  ;

    @Override
    public void run(ScheduleJob scheduleJob) {
        jobService job= SpringContextHolder.getBean(jobService.class);
        if (scheduleJob.getTaskType().contains("Procedure")) {
            JSONObject jsonObject = JSONObject.fromObject(scheduleJob.getParameter());
            String scriptInfo = jsonObject.getString("Script").replace("&gt", ">");
            String serviceName = jsonObject.getString("ServiceName");
            for (String s : serviceName.split(",")) {
                jdbcTemplate = (JdbcTemplate)SpringUtil.getApplicationContext().getBean(s) ;
                try {
                    for (String p : scriptInfo.split(";")) {
                        try {

                            try {
                                jdbcTemplate.execute("call " + p);
                                logger.info(String.format("存储过程【%s】执行完成", scheduleJob.getName()));
                            } catch (Exception e) {
                                logger.error(e.getMessage());
                            } finally {

                            }
                        } catch (Exception e) {
                            logger.error(String.format("存储过程【%s】执行失败", scheduleJob.getName()));
                            logger.error(p);
                            logger.error(e.getMessage());
                        }
                    }
                } catch (Exception ex) {
                    logger.error(ex.getMessage());
                }

            }
        } else {
            logger.error(String.format(String.format("【%s】任务参数错误，不是存储过程!", scheduleJob.getName())));
        }
    }
}
